using QFramework;
using UnityEngine;

namespace Survivor
{
    public class Global : Architecture<Global>
    {
        /// <summary>
        /// 生命值
        /// </summary>
        public static BindableProperty<int> Hp = new BindableProperty<int>(3);

        /// <summary>
        /// 最大生命值
        /// </summary>
        public static BindableProperty<int> MaxHp = new BindableProperty<int>(3);

        /// <summary>
        /// 经验值
        /// </summary>
        public static BindableProperty<int> Exp = new BindableProperty<int>();

        /// <summary>
        /// 金币
        /// </summary>
        public static BindableProperty<int> Coin = new BindableProperty<int>();

        /// <summary>
        /// 等级
        /// </summary>
        public static BindableProperty<int> Level = new BindableProperty<int>(1);

        /// <summary>
        /// 时间
        /// </summary>
        public static BindableProperty<float> CurrentSeconds = new BindableProperty<float>();

        /// <summary>
        /// SimpleAbility攻击力
        /// </summary>
        public static BindableProperty<int> SimpleAbilityDamage = new BindableProperty<int>(1);

        /// <summary>
        /// SimpleAbility频率
        /// </summary>
        public static BindableProperty<float> SimpleAbilityDuration = new BindableProperty<float>(1.5f);

        /// <summary>
        /// 金币掉落概率
        /// </summary>
        public static BindableProperty<float> CoinDropPercent = new BindableProperty<float>(0.1f);

        /// <summary>
        /// 经验掉落概率
        /// </summary>
        public static BindableProperty<float> ExpDropPercent = new BindableProperty<float>(0.3f);

        /// <summary>
        /// 重置数据
        /// </summary>
        public static void ResetData()
        {
            Hp.Value = MaxHp.Value;
            Exp.Value = 0;
            Level.Value = 1;
            CurrentSeconds.Value = 0;
            SimpleAbilityDamage.Value = 1;
            SimpleAbilityDuration.Value = 1.5f;
            EnemyGenerator.EnemyCnt.Value = 0;
        }

        [RuntimeInitializeOnLoadMethod]
        public static void AutoInit()
        {
            UIKit.Root.SetResolution(1920, 1080, 1);

            MaxHp.Value = PlayerPrefs.GetInt(nameof(MaxHp), 3);
            MaxHp.RegisterWithInitValue(maxHp => { PlayerPrefs.SetInt(nameof(MaxHp), MaxHp.Value); });

            Coin.Value = PlayerPrefs.GetInt(nameof(Coin), 0);
            Coin.RegisterWithInitValue(coin => { PlayerPrefs.SetInt(nameof(Coin), coin); });
            
            CoinDropPercent.Value = PlayerPrefs.GetFloat(nameof(CoinDropPercent), 0.1f);
            CoinDropPercent.RegisterWithInitValue(percent =>
            {
                PlayerPrefs.SetFloat(nameof(CoinDropPercent), percent);
            });

            ExpDropPercent.Value = PlayerPrefs.GetFloat(nameof(ExpDropPercent), 0.1f);
            ExpDropPercent.RegisterWithInitValue(percent => { PlayerPrefs.SetFloat(nameof(ExpDropPercent), percent); });

            ExpDropPercent.Value.LogInfo();
            CoinDropPercent.Value.LogInfo();
            ResetData();
        }

        /// <summary>
        /// 升到下一级所需要的经验
        /// </summary>
        public static int ExpToNextLevel()
        {
            return Level.Value * 5;
        }

        public static void GeneraterPowerUp(Vector3 pos)
        {
            float rand = Random.Range(0, 1f);
            $"掉落Exp {rand}".LogInfo();
            if (rand <= ExpDropPercent.Value)
            {
                PowerUpManager.Default.Exp.Instantiate()
                    .Position(pos)
                    .Show();
                return;
            }

            rand = Random.Range(0, 1f);
            $"掉落Coin {rand}".LogInfo();
            if (rand <= CoinDropPercent.Value)
            {
                PowerUpManager.Default.Coin.Instantiate()
                    .Position(pos)
                    .Show();
                return;
            }

            rand = Random.Range(0, 1f);
            $"掉落HP {rand}".LogInfo();
            if (rand <= 0.1f)
            {
                PowerUpManager.Default.HP.Instantiate()
                    .Position(pos)
                    .Show();
                return;
            }

            rand = Random.Range(0, 1f);
            $"掉落Bomb {rand}".LogInfo();
            if (rand < 0.1f)
            {
                PowerUpManager.Default.Bomb.Instantiate()
                    .Position(pos)
                    .Show();
                return;
            } 
            
            rand = Random.Range(0, 1f);
            $"掉落GetAllExp {rand}".LogInfo();
            if (rand < 0.1f)
            {
                PowerUpManager.Default.GetAllExp.Instantiate()
                    .Position(pos)
                    .Show();
                return;
            } 
        }

        protected override void Init()
        {
            // 模块的注册
            RegisterSystem(new CoinUpgradeSystem());
            RegisterSystem(new SaveSystem());
            RegisterSystem(new ExpUpgradeSystem());
        }
    }
}